import { AuthService } from '@core/services/common/auth.service';
import { HttpHandler, HttpInterceptor, HttpRequest, HttpResponse } from '@angular/common/http';
import { Injectable } from '@angular/core';
import { NzSafeAny } from 'ng-zorro-antd/core/types';

@Injectable()
export class AuthInterceptor implements HttpInterceptor {

  constructor(private auth: AuthService) { }

  intercept(req: HttpRequest<NzSafeAny>, next: HttpHandler) {
    console.log('auth-interceptor');
    const authToken: string = this.auth.getAuthorizationToken();
    let authConfig = {};
    if (!!authToken) {
      authConfig = {
        headers: req.headers.set('Authorization', authToken).set('Source-request','neurogin')
      };
    } else {
      authConfig = {
        headers: req.headers.set('Source-request','neurogin')
      };
    }
    const authReq = req.clone(authConfig);
    return next.handle(authReq);
  }
}
